<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule"  label-width="90px">
			<el-form-item label="参数名称" prop="paramsName">
				<el-input v-model="dataForm.paramsName" placeholder="请输入参数名称" readonly></el-input>
			</el-form-item>
			<el-form-item label="参数选项" prop="paramsOptions">
					<el-tag
						:key="tag"
						v-for="tag in dataForm.paramsOptions"
						closable
						:disable-transitions="false"
						@close="handleClose(tag)">
						{{tag}}
					</el-tag>
					<el-input
						:class="{'button-new-tag':(dataForm.paramsOptions.length > 0)}"
						class="input-null-tag"
						v-if="inputVisible"
						v-model="inputValue"
						ref="saveTagInput"
						size="small"
						@keyup.enter.native="handleInputConfirm"
						@blur="handleInputConfirm"
					>
					</el-input>
					<el-button v-else :class="{'button-new-tag':(dataForm.paramsOptions.length > 0)}" class="button-null-tag" size="small" @click="showInput">添加参数</el-button>
			</el-form-item>
			<el-form-item label="参数排序" prop="paramsSort">
				<el-input v-model="dataForm.paramsSort" placeholder="请输入参数排序"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { editGoodsParameters,findGoodsParametersDetail } from '@/api/mall/goodsParameters'
	export default {
		name: 'add-goodsParameters',
		data() {
			return {
				dataForm: {
					id:'',
					paramsName: '',
					paramsOptions: [],
					templateCode: '',
					paramsSort: '',
				},
				inputVisible: false,
				inputValue: '',
				dataRule: {
					paramsOptions: [{
							required: true,
							message: '请输入参数选项',
							trigger: 'blur'
						},
						
					],
				}
			}
		},
		created() {
			this.dataForm.id = this.$route.query.id
			this.view(this.$route.query.id)
		},
		methods: {
			view(val) {
				findGoodsParametersDetail(val).then(res => {
					this.dataForm = res.data.responseData
				})
			},
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					editGoodsParameters(this.dataForm).then(() => {
						this.$notify({
							title: '修改成功',
							message: '修改成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			},
			handleClose(tag) {
				this.dataForm.paramsOptions.splice(this.dataForm.paramsOptions.indexOf(tag), 1);
			},
			showInput() {
				this.inputVisible = true;
				this.$nextTick(() => {
					this.$refs.saveTagInput.$refs.input.focus();
				});
			},
			handleInputConfirm() {
				let inputValue = this.inputValue;
				if (inputValue) {
					if(this.dataForm.paramsOptions.indexOf(inputValue.trim()) == '-1'){
						this.dataForm.paramsOptions.push(inputValue.trim());
					}
				}
				this.inputVisible = false;
				this.inputValue = '';
			}
		}
	}
</script>

<style>
.el-tag + .el-tag {
	margin-left: 10px;
  }
  .button-null-tag{
	height: 32px;
	line-height: 30px;
	padding-top: 0;
	padding-bottom: 0;
  }
  
  .button-new-tag {
	margin-left: 10px;
	height: 32px;
	line-height: 30px;
	padding-top: 0;
	padding-bottom: 0;
  }
  
  .input-new-tag {
	width: 90px;
	margin-left: 10px;
	vertical-align: bottom;
  }
  
  .input-null-tag{
		width: 90px;
		vertical-align: bottom;
  }
</style>
